package com.rui1024.servlet;

import com.rui1024.dao.UserDao;
import com.rui1024.dao.impl.UserDaoImpl;
import com.rui1024.util.JDBCUtil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/AccountServlet")
public class AccountServlet extends HttpServlet {


	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");

        UserDao userDao=new UserDaoImpl();

		JDBCUtil jdbcUtil = new JDBCUtil();
		Connection con = null;
		String deletename=null;

		try {
			con = jdbcUtil.getCon();
			List list =userDao.usermanlist(con);

			if(req.getParameter("deletename") != null){
				deletename = req.getParameter("deletename");
				userDao.deleteuser(deletename,con);
			}

			req.setAttribute("accounts",list);
			req.getRequestDispatcher("account.jsp").forward(req, resp);

		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				jdbcUtil.closeCon(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

			

	}
 	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
}